package com.gbb.common.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.text.Spanned;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * ListView适配器基类
 *
 * @author gaobingbing91
 */
public abstract class AbstractAdapter<T> extends BaseAdapter {

    protected Context mContext;

    protected List<T> dataList;

    protected LayoutInflater mInflater;

    public AbstractAdapter(Context mContext,List<T> dataList){
        this.mContext = mContext;
        this.dataList = dataList;
        mInflater = (LayoutInflater) this.mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @Override
    public int getCount(){
        return dataList == null ? 0 : dataList.size();
    }

    @Override
    public T getItem(int position){
        return dataList == null ? null : dataList.get(position);
    }

    @Override
    public long getItemId(int position){
        return position;
    }

    @Override
    public abstract View getView(int position,View convertView,ViewGroup parent);

    public void setDataList(List<T> dataList){
        this.dataList = dataList;
        notifyDataSetChanged();
    }

    public List<T> getDataList(){
        return dataList;
    }

    protected String getString(int stringRes){
        return mContext.getString(stringRes);
    }

    protected String getString(int stringRes,Object... formatArgs){
        return mContext.getString(stringRes,formatArgs);
    }

    protected int getColor(int colorRes){
        return mContext.getResources().getColor(colorRes);
    }

    protected Spanned getHtml(int stringRes){
        return Html.fromHtml(getString(stringRes));
    }

    protected Spanned getHtml(int stringRes,Object... formatArgs){
        return Html.fromHtml(getString(stringRes,formatArgs));
    }

    protected void startActivity(Intent mIntent){
        mContext.startActivity(mIntent);
    }

    protected void startActivity(Class<? extends Activity> targetClazz){
        mContext.startActivity(new Intent(mContext,targetClazz));
    }
}
